package com.attendance.www.hr.service.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.attendance.www.hr.beans.RoleMainBean;
import com.attendance.www.hr.dao.mapper.DepartmentMapper;
import com.attendance.www.hr.dao.mapper.RoleDepartmentMapper;
import com.attendance.www.hr.dao.mapper.RoleExtMapper;
import com.attendance.www.hr.dao.mapper.RoleMapper;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.entitys.DepartmentExample;
import com.attendance.www.hr.entitys.Role;
import com.attendance.www.hr.entitys.RoleDepartment;
import com.attendance.www.hr.entitys.RoleDepartmentExample;
import com.attendance.www.hr.entitys.RoleExample;
import com.attendance.www.hr.service.IRoleMainService;

@Service
public class RoleMainServiceImpl implements IRoleMainService {
	@Resource
	private RoleMapper roleDao;
	@Resource
	private RoleExtMapper roleExtDao;
	@Resource
	private DepartmentMapper departmentDao;
	@Resource
	private RoleDepartmentMapper roleDepartmentDao;
	/**
	 * 岗位对接
	 */
	@Override
	public void saveRoleMain(List<RoleMainBean> roleMainBeanList) {
		//查询开始日期
		Date fromDate=roleExtDao.selectRoleMinFromDate();
		
		//查询岗位表存储到集合里
		List<Role> roleList=roleDao.selectByExample(new RoleExample());
		LinkedHashMap<String, Role> roleMap = new LinkedHashMap<String, Role>();
		for(int ri=0;ri<roleList.size();ri++) {
			roleMap.put(roleList.get(ri).getRoleCode(), roleList.get(ri));
		}
		
		//查询部门
		List<Department> departmentList=departmentDao.selectByExample(new DepartmentExample());
		LinkedHashMap<String, Department> departmentMap = new LinkedHashMap<String,Department>();
		for(int di=0;di<departmentList.size();di++ ) {
			departmentMap.put(departmentList.get(di).getDepartmentCode(), departmentList.get(di));
		}
		
		//查询岗位和部门
		List<RoleDepartment> roleDepartmentList=roleDepartmentDao.selectByExample(new RoleDepartmentExample());
		LinkedHashMap<String, RoleDepartment> roleDepartmentMap = new LinkedHashMap<String,RoleDepartment>();
		for(int rdi=0;rdi<roleDepartmentList.size();rdi++) {
			roleDepartmentMap.put(roleDepartmentList.get(rdi).getRoleId()+"#"+roleDepartmentList.get(rdi).getDepartmentId(), roleDepartmentList.get(rdi));
		}
		
		//岗位主数据信息
		for(int i=0;i<roleMainBeanList.size();i++) {
			RoleMainBean roleMainBean=roleMainBeanList.get(i);
			Role role =roleMap.get(roleMainBean.getJobsCode());
			if(role!=null) {
				//判断是否有上级
				if(StringUtils.isNotBlank(roleMainBean.getSuperiorCode())) {
					Role roleParent=roleMap.get(roleMainBean.getSuperiorCode());
					if(roleParent!=null) {
						role.setParentId(roleParent.getRoleId());
					}
				}
				
				role.setRoleCode(roleMainBean.getJobsCode());
				role.setRoleName(roleMainBean.getJobsName());
				role.setDescription(roleMainBean.getRemark());
				
				//启动状态
				if(StringUtils.isNotBlank(roleMainBean.getOpenStatus())) {
					if(roleMainBean.getOpenStatus().equals("2")||roleMainBean.getOpenStatus().equals("3")) {
						role.setIsDisabled(1);
						role.setDisabledTime(new Date());
					}else {
						role.setIsDisabled(0);
					}
				}
				//操作区分
				if(StringUtils.isNotBlank(roleMainBean.getActionFlag())) {
					if(roleMainBean.getActionFlag().equals("2")||roleMainBean.getActionFlag().equals("6")) {
						role.setIsDisabled(1);	
						role.setDisabledTime(new Date());
					}else if(roleMainBean.getActionFlag().equals("3")) {
						role.setIsDisabled(0);
					}
				}
				
				role.setUpdateTime(new Date());
				
				//修改
				roleDao.updateByPrimaryKeySelective(role);
				
				if(role.getIsDisabled()==0) {
					roleExtDao.updateDisabledTimeByRoleId(role.getRoleId());
				}
				
				//添加部门关系
				Department department=departmentMap.get(roleMainBean.getOrganCode());
				if(department!=null) {
					//验证是否更改所属部门
					String key=role.getRoleId()+"#"+department.getDepartmentId();
					RoleDepartment roleDepartment=roleDepartmentMap.get(key);
					//更改所属部门
					if(roleDepartment==null) {
						//删除原有岗位下关系数据，重新插入
						RoleDepartmentExample roleDepartmentExample=new RoleDepartmentExample();
						roleDepartmentExample.createCriteria().andRoleIdEqualTo(role.getRoleId());
						roleDepartmentDao.deleteByExample(roleDepartmentExample);
						
						//实例化对象赋值
						roleDepartment=new RoleDepartment();
						roleDepartment.setRoleId(role.getRoleId());
						roleDepartment.setDepartmentId(department.getDepartmentId());
						roleDepartment.setFromDate(role.getFromDate());
						roleDepartmentDao.insertSelective(roleDepartment);
					}
				}
			}else {
				role = new Role();
				//增加
				role.setRoleCode(roleMainBean.getJobsCode());
				role.setRoleName(roleMainBean.getJobsName());
				role.setDescription(roleMainBean.getRemark());
				role.setFromDate(fromDate);
				
				//判断是否有上级
				if(StringUtils.isNotBlank(roleMainBean.getSuperiorCode())) {
					Role roleParent=roleMap.get(roleMainBean.getSuperiorCode());
					if(roleParent!=null) {
						role.setParentId(roleParent.getRoleId());
					}
				}
				
				//判断启用状态
				if(StringUtils.isNotBlank(roleMainBean.getOpenStatus())) {
					if(roleMainBean.getOpenStatus().equals("2") || roleMainBean.getOpenStatus().equals("3")) {
						role.setIsDisabled(1);
						role.setDisabledTime(new Date());
					}else {
						role.setIsDisabled(0);
					}
				}
				
				//操作区分
				if(StringUtils.isNotBlank(roleMainBean.getActionFlag())) {
					if(roleMainBean.getActionFlag().equals("2") || roleMainBean.getActionFlag().equals("6")) {
						role.setIsDisabled(1);
						role.setDisabledTime(new Date());
					}else if(roleMainBean.getActionFlag().equals("3")) {
						role.setIsDisabled(0);
					}
				}
				
				//添加
				roleDao.insertSelective(role);
				
				if(role.getIsDisabled()==0) {
					roleExtDao.updateDisabledTimeByRoleId(role.getRoleId());
				}
				
				//通过RoleCode取出JobsCode
				roleMap.put(role.getRoleCode(), role);
				
				//添加部门关系
				Department department=departmentMap.get(roleMainBean.getOrganCode());
				if(department!=null) {
					//插入岗位和部门关系表
					RoleDepartment roleDepartment=new RoleDepartment();
					roleDepartment.setRoleId(role.getRoleId());
					roleDepartment.setDepartmentId(department.getDepartmentId());
					roleDepartment.setFromDate(fromDate);
					roleDepartmentDao.insertSelective(roleDepartment);
				}
			}	
		}
	}
}
